package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.EmployeeConstant;
import com.itheima.domain.Employee;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
/**
 * @Author zhangwenjie
 * @Desc: 首页员工管理
 * @Date 12:28 2022/6/5
 * @Version 1.0
 **/
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    public EmployeeMapper employeeMapper;

    @Override
    public Employee login(Employee employee) {
        Employee emp = null;
        //判断传入的employee是否为空
        if (employee==null){
            return emp;
        }
        //获取前端传递的username和password
        String username = employee.getUsername();
        String password = employee.getPassword();
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //根据username查找用户信息
        wrapper.eq(Employee::getUsername, username);
        Employee selectOne = employeeMapper.selectOne(wrapper);
        //如果查找的用户为空,说明该用户名没有对应的用户信息
        if (selectOne==null){
            return emp;
        }
        //把密码用MD5格式加密
        String encodePwd  = DigestUtils.md5DigestAsHex(password.getBytes());
        //对比密码和查找的用户密码是否一致
        if (!encodePwd.equals(selectOne.getPassword())){
            return emp;
        }
        emp=selectOne;
        //把用户信息返回
        return emp;
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> page(Integer page, Integer pageSize, String name) {
        //判断当前页 如果小于小 则显示1页
        if (page < 1) {
            page = 1;
        }
        //判断每页条数小于10 则每页最大显示10条
        if (pageSize < 1) {
            pageSize = 10;
        }
        //封装查询
        Page<Employee> employeePage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //如果搜索框里的搜索不为空等 按照输入结果模糊查询
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(Employee::getName,name);
        }
        //执行查询结果
        Page<Employee> employeeIPage = employeeMapper.selectPage(employeePage,wrapper);

        //返回查询结果
        return employeeIPage;
    }

    /**
     * 新增员工逻辑
     *
     * @param employee
     * @return
     */
    @Override
    public int empadd(Employee employee) {
        int row = 0;
        //获取Username,判断是否为空 "" null
        String username = employee.getUsername();
        if (StringUtils.isBlank(username)) {
            return row;
        }
        //查询用户
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, username);
        //查询结果
        Employee one = employeeMapper.selectOne(wrapper);
        //如果不为空,则证明新增用户名已有 返回row
        if (one != null) {
            return row;
        }
        employee.setPassword(DigestUtils.md5DigestAsHex(EmployeeConstant.EMP_INIT_PASSWORD.getBytes()));
        row = employeeMapper.insert(employee);
        return row;

    }

    /**
     * 员工回显逻辑实现
     *
     * @param id
     * @return
     */
    @Override
    public Employee findById(Long id) {
        //校验判断 如果id为空 则返回空
        if (id == null) {
            return null;
        }
        //查询ID 不为空 则返回对象接收
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }

    /**
     * 员工状态逻辑实现
     * @param employee
     * @return
     */
    @Override
    public int states(Employee employee) {
        int row =0;
        //判断
        if (employee == null) {
            return row;
        }
        //判断用户是否为管理员
        Long currentId = BaseContext.getCurrentId();
        if (currentId == null) {
            return row;
        }
        //执行修改员工信息
        row = employeeMapper.updateById(employee);
        return row;
    }


}